<?php


namespace app\admin\model;


use think\Model;

class Cate extends Model
{
    public function catetree(){
        $cateres = self::order('sort desc')->all();
        $res = $this->sort($cateres);
        return $res;
    }
    public function sort($cateres,$pid=0,$level=0){
        static $arr = array();
        foreach($cateres as $k=>$v){
            if($v['pid']==$pid){
                $v['level'] = $level;
                $arr[] = $v;
                $this->sort($cateres,$v['id'],$level+1);
            }
        }
        return $arr;
    }
    public function modify($data,$id){
        if(empty($data)||!is_array($data)){
            return 1;
        }
        $res = $this->save($data,['id'=>$id]);
        if($res){
            return 2;
        }else{
            return 3;
        }
    }
    public function getchildrenid($cateid){
        $cateres = $this->select();

        return $this->_getchildrenid($cateres,$cateid);
    }
    public function _getchildrenid($cateres,$cateid){
        static $arr = array();

        foreach($cateres as $k=>$v){
            if($v['pid']==$cateid){
                $arr[] = $v['id'];
                $this->_getchildrenid($cateres,$v['id']);
            }
        }
        return $arr;
    }
}